/*
 * main.cpp
 *
 *  Created on: Mar 19, 2012
 *      Author: s147614
 */

#include "BigBrother.h"

BigBrother::BigBrother() {
	cNoord = new Condition();
	cOost = new Condition();
	cZuid = new Condition();
	cWest = new Condition();
	cPasseerd = new Condition();

	mNoord = new Mutex();
	mOost = new Mutex();
	mZuid = new Mutex();
	mWest = new Mutex();

	wachtendeNoord = 0;
	wachtendeOost = 0;
	wachtendeZuid = 0;
	wachtendeWest = 0;
}

BigBrother::~BigBrother() {
	delete cNoord;
	delete cOost;
	delete cZuid;
	delete cWest;
	delete cPasseerd;

	delete mNoord;
	delete mOost;
	delete mZuid;
	delete mWest;
}

void BigBrother::verlaagTeller(Richting deRichting) {
	switch (deRichting) {
	case NOORD:
		wachtendeNoord--;
		break;
	case OOST:
		wachtendeOost--;
		break;
	case ZUID:
		wachtendeZuid--;
		break;
	case WEST:
		wachtendeWest--;
		break;
	}
}

void BigBrother::meldAan(Auto* autootje) {
	switch (autootje->getRichting()) {
	case NOORD:
		mNoord->lock();
		autootje->setCRichting(cNoord);
		autootje->setmRichting(mNoord);
		wachtendeNoord++;
		mNoord->unlock();
		break;
	case OOST:
		mOost->lock();
		autootje->setCRichting(cOost);
		autootje->setmRichting(mOost);
		wachtendeOost++;
		mOost->unlock();
		break;
	case ZUID:
		mZuid->lock();
		autootje->setCRichting(cZuid);
		autootje->setmRichting(mZuid);
		wachtendeZuid++;
		mZuid->unlock();
		break;
	case WEST:
		mWest->lock();
		autootje->setCRichting(cWest);
		autootje->setmRichting(mWest);
		wachtendeWest++;
		mWest->unlock();
		break;
	}
	autootje->setCPasseerd(cPasseerd);
	autootje->start();
}

Condition* BigBrother::getNoord() {
	return cNoord;
}

Condition* BigBrother::getOost() {
	return cOost;
}

Condition* BigBrother::getWest() {
	return cWest;
}

Condition* BigBrother::getZuid() {
	return cZuid;
}

Condition* BigBrother::getPasseerd() {
	return cPasseerd;
}

Mutex* BigBrother::getMutexNoord() {
	return mNoord;
}

Mutex* BigBrother::getMutexOost() {
	return mOost;
}

Mutex* BigBrother::getMutexZuid() {
	return mZuid;
}

Mutex* BigBrother::getMutexWest() {
	return mWest;
}

int BigBrother::getWachtendeNoord() {
	return wachtendeNoord;
}

int BigBrother::getWachtendeOost() {
	return wachtendeOost;
}

int BigBrother::getWachtendeZuid() {
	return wachtendeZuid;
}

int BigBrother::getWachtendeWest() {
	return wachtendeWest;
}

